<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template slot-scope="{row}"
                slot="storeName">
        <el-tag>{{row.storeName}}</el-tag>
      </template>
      <template slot-scope="{row}"
                slot="commodityType">
        <el-tag>{{row.commodityType}}</el-tag>
      </template>
      <template slot="menu" slot-scope="{row}">
        <el-button type="text"
                   size="small"
                   icon="el-icon-view"
                   @click="commodityView(row)">查看
        </el-button>
        <el-button type="text"
                   size="small"
                   icon="el-icon-search"
                   @click="dialog(row)">商品参数
        </el-button>
      </template>
    </avue-crud>
    <el-dialog
      title="详细参数"
      :visible.sync="dialogVisible"
      width="60%"
      :before-close="handleClose">
      <avue-form ref="paramsForm" v-model="paramsData" :option="optionParams" @submit="submit">
      </avue-form>
    </el-dialog>
    <el-drawer
      title="查看商品"
      size="50%"
      :visible.sync="drawer"
      :direction="direction"
      :before-close="handleClose">
      <div style="width: 90%;margin: 0 auto">
        <span>商店名称：{{commodityAll.storeName}}</span><br>
        <span>商品名称：{{commodityAll.commodityName}}</span><br>
        <span>商品类型：{{commodityAll.commodityType}}</span><br>
        <span>商品价格：{{commodityAll.commodityPrice}}</span><br>
        <span>商品规格：{{commodityAll.specs}}</span><br>
        <span>商品介绍：{{commodityAll.commodityIntroduce}}</span><br>
        <span>商品地址：{{commodityAll.address}}</span><br>
        <div style="width: 100px;height: 150px">商品图片:
        <img style="width: 100px;height: 100px" :src="commodityAll.commodityImg" class="image" alt="图片不见了"><br>
        </div>
        商品详细图片:
        <el-image style="width: 100px;height: 100px" v-for="url in urls" :key="url" :src="url"/><br><br>
        <div>
        <span style="padding-left: 10%;font-size: 15px">图案:{{commodityParams.pattern}}</span>
        <span style="padding-left: 10%;font-size: 15px">细分风格:{{commodityParams.subdivisionStyle}}</span>
        <span style="padding-left: 10%;font-size: 15px">服饰工艺:{{commodityParams.technology}}</span><br>
        <span style="padding-left: 10%;font-size: 15px">面料分类:{{commodityParams.fabrics}}</span>
        <span style="padding-left: 10%;font-size: 15px">基础风格:{{commodityParams.basic}}</span>
        <span style="padding-left: 10%;font-size: 15px">上市年份季节:{{commodityParams.season}}</span><br>
        <span style="padding-left: 10%;font-size: 15px">厚薄:{{commodityParams.thickness}}</span>
        <span style="padding-left: 10%;font-size: 15px">销售渠道类型:{{commodityParams.channel}}</span>
        <span style="padding-left: 10%;font-size: 15px">材质成分:{{commodityParams.material}}</span><br>
        <span style="padding-left: 10%;font-size: 15px">适用场景:{{commodityParams.scene}}</span>
        <span style="padding-left: 10%;font-size: 15px">适用对象:{{commodityParams.objects}}</span>
        </div>
      </div>
    </el-drawer>
  </basic-container>
</template>

<script>
  import {getDetail, add, update, remove, commodityPage} from "@/api/commodity/commodity";
  import {add as addParams,getCommodityIdDetail} from "@/api/params/commodityparams";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        urls: [],
        drawer: false,
        direction: 'rtl',
        dialogVisible: false,
        form: {},
        paramsForm:{},
        paramsData: {},
        commodityAll:{},
        commodityParams:{},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: false,
          selection: true,
          column: [
            {
              label: "商家名称",
              prop: "storeName",
              slot: true,
              addDisplay: false,
              editDisplay: false,
            },
            {
              label: "商品名称",
              prop: "commodityName",
              search: true,
              rules: [{
                required: true,
                message: "请输入商品名称",
                trigger: "blur"
              }]
            },
            {
              label: "商品类型",
              prop: "commodityType",
              type:'select',
              slot: true,
              props: {
                label: 'dictValue',
                value: 'dictKey'
              },
              dicUrl:"/api/pxx_commodity/commodity/commodityType"
            },
            {
              label: "商品价格",
              prop: "commodityPrice",
              tip:'请输入商品价格(请用/分割,如:1/2)',
              rules: [{
                required: true,
                message: "请输入商品价格(请用/分割,如:1/2)",
                trigger: "blur"
              }]
            },
            {
              label: "商品规格",
              prop: "specs",
              tip:'请输入商品规格(请用/分割,如:豪华版/标准版)',
              rules: [{
                required: true,
                message: "请输入商品规格(请用/分割,如:豪华版/标准版)",
                trigger: "blur"
              }]
            },
            {
              label: "商品图片",
              prop: "commodityImg",
              span: 24,
              propsHttp: {
                res: 'data'
              },
              type: 'upload',
              listType: 'picture-img',
              tip: '只能上传jpg/png用户头像，且不超过500kb',
              action: '/api/pxx_commodity/commodity/upload',
            },
            {
              label: "商品详细图",
              prop: "commodityPicture",
              type: 'upload',
              span: 24,
              dataType: "string",
              propsHttp: {
                res: 'data'
              },
              listType: 'picture-card',
              action: '/api/pxx_commodity/commodity/upload',
            },
            {
              label: "商品介绍",
              prop: "commodityIntroduce",
              rules: [{
                required: true,
                message: "请输入商品介绍",
                trigger: "blur"
              }]
            },
            {
              label: "地址",
              prop: "address",
              rules: [{
                required: true,
                message: "请输入地址",
                trigger: "blur"
              }]
            },
          ]
        },
        optionParams: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          column: [
            {
              label: "图案",
              prop: "pattern",
              rules: [{
                required: true,
                message: "请输入图案",
                trigger: "blur"
              }]
            },
            {
              label: "细分风格",
              prop: "subdivisionStyle",
              rules: [{
                required: true,
                message: "请输入细分风格",
                trigger: "blur"
              }]
            },
            {
              label: "服饰工艺",
              prop: "technology",
              rules: [{
                required: true,
                message: "请输入服饰工艺",
                trigger: "blur"
              }]
            },
            {
              label: "面料分类",
              prop: "fabrics",
              rules: [{
                required: true,
                message: "请输入面料分类",
                trigger: "blur"
              }]
            },
            {
              label: "基础风格",
              prop: "basic",
              rules: [{
                required: true,
                message: "请输入基础风格",
                trigger: "blur"
              }]
            },
            {
              label: "上市年份季节",
              prop: "season",
              rules: [{
                required: true,
                message: "请输入上市年份季节",
                trigger: "blur"
              }]
            },
            {
              label: "厚薄",
              prop: "thickness",
              rules: [{
                required: true,
                message: "请输入厚薄",
                trigger: "blur"
              }]
            },
            {
              label: "销售渠道类型",
              prop: "channel",
              rules: [{
                required: true,
                message: "请输入销售渠道类型",
                trigger: "blur"
              }]
            },
            {
              label: "材质成分",
              prop: "material",
              rules: [{
                required: true,
                message: "请输入材质成分",
                trigger: "blur"
              }]
            },
            {
              label: "适用场景",
              prop: "scene",
              rules: [{
                required: true,
                message: "请输入适用场景",
                trigger: "blur"
              }]
            },
            {
              label: "适用对象",
              prop: "objects",
              rules: [{
                required: true,
                message: "请输入适用对象",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.commodity_add, false),
          viewBtn: this.vaildData(this.permission.commodity_view, false),
          delBtn: this.vaildData(this.permission.commodity_delete, false),
          editBtn: this.vaildData(this.permission.commodity_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      commodityView(row){
        this.drawer=true
        var id = row.id;
        getDetail(id).then(res => {
          this.commodityAll=res.data.data;
          this.commodityParams=res.data.data.commodityParams;
          this.urls=res.data.data.commodityPicture.split(",");
        });
      },
      dialog(row){
        this.dialogVisible=true;
        this.paramsData.commodityId=row.id
        getCommodityIdDetail(row.id).then(res => {
          this.paramsData = res.data.data;
        });
      },
      submit(data,done, loading){
        var row = this.paramsData;
        addParams(row).then(() => {
          done();
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          loading();
          window.console.log(error);
        });
        this.dialogVisible=false;
      },
      rowSave(row, done, loading) {
         if (row.commodityImg===''){
           this.$message.error("请上传商品图片!");
           done();
           return
         }
        if (row.commodityImg===[]){
          this.$message.error("请上传商品详细图片!");
          done();
          return
        }
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        commodityPage(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
